package com.kiven.thread.lock;

import java.util.*;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

public class CacheDemo {
	private Map<String,Object> map = new HashMap<String,Object>();
	private ReadWriteLock rwl = new ReentrantReadWriteLock();
	
	public Object get(String key) {
		rwl.readLock().lock();
		Object x = null;
		try {
			if (map.get(key) == null) {
				rwl.readLock().unlock();
				rwl.writeLock().lock();
				try {
					if (map.get(key) == null) {
						x = "this is a test";
					}
				} finally {
					rwl.writeLock().unlock();
				}
				rwl.readLock().lock();
			}
		} finally {
			rwl.readLock().unlock();
		}
		return x;
	}
}
